﻿using GalaSoft.MvvmLight.CommandWpf;
using PropertyChanged;
using System;
using System.Runtime.Serialization;
using System.Windows.Input;
using Gdi = System.Drawing;

namespace GEPL.ViewModels
{
    /// <summary>
    /// Model used to transfer settings for new turtle graphics project
    /// </summary>
    [Serializable]
    [ImplementPropertyChanged]
    public class TurtleProjectSettingsModel : ISerializable
    {
        public ICommand OkCommand { get; set; }

        /// <summary>
        /// Width of an image
        /// </summary>
        public int Width { get; set; }

        /// <summary>
        /// Height of an image
        /// </summary>
        public int Height { get; set; }

        /// <summary>
        /// Pen color
        /// </summary>
        public Gdi.Color PenColor { get; set; }

        /// <summary>
        /// Background color
        /// </summary>
        public Gdi.Color BgColor { get; set; }

        /// <summary>
        /// Creates instance of class.
        /// Initializes default values.
        /// </summary>
        public TurtleProjectSettingsModel()
        {
            OkCommand = new RelayCommand(SaveSettings);

            Width = Properties.Settings.Default.TurtleProjectSettings_Width;
            Height = Properties.Settings.Default.TurtleProjectSettings_Height;
            PenColor = Properties.Settings.Default.TurtleProjectSettings_PenColor;
            BgColor = Properties.Settings.Default.TurtleProjectSettings_BgColor;
        }

        public TurtleProjectSettingsModel(SerializationInfo info, StreamingContext context)
        {
            OkCommand = new RelayCommand(SaveSettings);

            Width = info.GetInt32(nameof(Width));
            Height = info.GetInt32(nameof(Height));
            PenColor = (Gdi.Color)info.GetValue(nameof(PenColor), typeof(Gdi.Color));
            BgColor = (Gdi.Color)info.GetValue(nameof(BgColor), typeof(Gdi.Color));
        }

        public void SaveSettings()
        {
            Properties.Settings.Default.TurtleProjectSettings_Width = Width;
            Properties.Settings.Default.TurtleProjectSettings_Height = Height;
            Properties.Settings.Default.TurtleProjectSettings_PenColor = PenColor;
            Properties.Settings.Default.TurtleProjectSettings_BgColor = BgColor;
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(nameof(Width), Width);
            info.AddValue(nameof(Height), Height);
            info.AddValue(nameof(PenColor), PenColor, typeof(Gdi.Color));
            info.AddValue(nameof(BgColor), BgColor, typeof(Gdi.Color));
        }
    }
}